package com.live.dengdengtv.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.aigestudio.wheelpicker.WheelPicker;
import com.aigestudio.wheelpicker.widgets.WheelDatePicker;
import com.google.gson.Gson;
import com.live.dengdengtv.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wyf on 2016/8/11.
 */
public class AddressPickerDialog {


    private Dialog pickDialog;
    private Context context;

    private OnWheelAddressDismiss onWheelAddressDismiss;
    private WheelPicker wheel_province;
    private WheelPicker wheel_city;

    public void showDatePickerDialog(Activity activity, OnWheelAddressDismiss onWheelAddressDismiss) {

        context = activity;
        this.onWheelAddressDismiss = onWheelAddressDismiss;

        pickDialog = new Dialog(activity, R.style.dialog_float);
        pickDialog.setContentView(R.layout.dialog_addresspicker);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window dlgwin = pickDialog.getWindow();
        WindowManager.LayoutParams lp = dlgwin.getAttributes();
        dlgwin.setGravity(Gravity.BOTTOM);
        lp.width = (int)(display.getWidth()); //设置宽度

        pickDialog.getWindow().setAttributes(lp);




        parsePJson();
        parseCJson();
        Log.e("AddressPickerDialog", provinceList.toString());


        wheel_province = (WheelPicker) pickDialog.findViewById(R.id.wheel_province);
//        wheel_province.setCyclic(false);
        wheel_province.setData(provinceList);

        wheel_city = (WheelPicker) pickDialog.findViewById(R.id.wheel_city);
        wheel_city.setCyclic(false);//循环显示
        wheel_city.setData(cityMap.get("北京市"));

        wheel_province.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                wheel_city.setData(cityMap.get(provinceList.get(position)));

            }
        });



        pickDialog.setOnDismissListener(onDismissListener);
        pickDialog.show();

    }


    private List<String> provinceList = new ArrayList<>();
    private Map<String,List<String>> cityMap = new HashMap<>();
    private Map<String,String> idMap = new HashMap<>();
    private Map<String,String> provinceMap = new HashMap<>();

    private void parsePJson(){
        String provinceJson = readJsonData("dd_province.json");
        try {
            JSONObject provinceJsonObj = new JSONObject(provinceJson);
            JSONArray jsonArray = provinceJsonObj.getJSONArray("RECORDS");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonP = jsonArray.getJSONObject(i);
                provinceList.add(jsonP.getString("name"));
                idMap.put(jsonP.getString("name"),jsonP.getString("id"));
                provinceMap.put(jsonP.getString("id"),jsonP.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void parseCJson(){
        String provinceJson = readJsonData("dd_city.json");
        try {
            JSONObject provinceJsonObj = new JSONObject(provinceJson);
            JSONArray jsonArray = provinceJsonObj.getJSONArray("RECORDS");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonP = jsonArray.getJSONObject(i);
                String province_id = jsonP.getString("province_id");
                String cityName = jsonP.getString("name");
                String cityId = jsonP.getString("id");
                String provinceName = provinceMap.get(province_id);
                List<String> cityList = cityMap.get(provinceName);
                if(cityList == null){
                    cityList = new ArrayList<>();
                    cityMap.put(provinceName, cityList);
                }
                cityList.add(cityName);
                idMap.put(cityName,cityId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从文件中读取地址数据
     */
    private String readJsonData(String fileName) {
        StringBuffer sb = new StringBuffer();
        try {
            InputStream is = context.getAssets().open(fileName);
            int len = -1;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len, "gbk"));
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {


            int pIndex = wheel_province.getCurrentItemPosition();
            int cIndex = wheel_city.getCurrentItemPosition();
            String p = provinceList.get(pIndex);
            String city = cityMap.get(p).get(cIndex);

            onWheelAddressDismiss.onWheelAddressDismiss(p,city,idMap.get(p), idMap.get(city));
            onWheelAddressDismiss = null;
        }
    };


    public interface OnWheelAddressDismiss {
        void onWheelAddressDismiss(String p,String c, String pId,String cId);
    }

}
